/* eslint-disable no-shadow */

/* eslint-disable max-classes-per-file */
import { ifs } from '@/services';
import { N_oSimpleToolbar } from '@/types';
import Vue from 'vue';

import {
  I_formPanel,
  I_tablePanel,
  T_baseGrid,
  T_baseGridConf,
  T_basePanel,
  T_basePanelConf,
  T_baseTab,
  T_baseTabConf,
  T_baseView,
  T_baseViewConf,
  T_cBaseForm,
  T_cBaseFormConf,
  T_cloneBasePanel,
  T_cloneBasePanelConf,
  T_dateNav,
  T_dateNavConf,
  T_graph,
  T_graphConf,
  T_multiForm,
  T_multiFormConf,
  T_refBaseGrid,
  T_refBaseGridConf,
  T_refComp,
  T_refCompConf,
  T_refGraph,
  T_refGraphConf,
  T_reportChart,
  T_reportChartConf,
  T_treeGrid,
  T_treeGridConf
} from './command/comps/types';

export namespace N_oSetupPanel {
  export namespace N_comps {
    /// ////////////////////////////////////////////////////////////////

    export interface I_baseGridConf extends T_baseGridConf {}
    export interface I_baseGrid extends T_baseGrid {}
    export interface I_basePanelConf extends T_basePanelConf {}
    export interface I_basePanel extends T_basePanel {}
    export type I_basePanelFormPanel = I_formPanel;
    export type I_basePanelTablePanel = I_tablePanel;

    export interface I_baseTabConf extends T_baseTabConf {}
    export interface I_baseTab extends T_baseTab {}
    export interface I_graphConf extends T_graphConf {}
    export interface I_graph extends T_graph {}
    export interface I_treeGridConf extends T_treeGridConf {}
    export interface I_treeGrid extends T_treeGrid {}
    export interface I_dateNavConf extends T_dateNavConf {}
    export interface I_dateNav extends T_dateNav {}
    export interface I_cBaseFormConf extends T_cBaseFormConf {}
    export interface I_cBaseForm extends T_cBaseForm {}
    export interface I_baseViewConf extends T_baseViewConf {}
    export interface I_baseView extends T_baseView {}
    export interface I_refComp extends T_refComp {}
    export interface I_refCompConf extends T_refCompConf {}
    export interface I_multiForm extends T_multiForm {}
    export interface I_multiFormConf extends T_multiFormConf {}
    export interface I_refGraph extends T_refGraph {}
    export interface I_refGraphConf extends T_refGraphConf {}
    export interface I_refBaseGridConf extends T_refBaseGridConf {}
    export interface I_refBaseGrid extends T_refBaseGrid {}
    export interface I_reportChartConf extends T_reportChartConf {}
    export interface I_reportChart extends T_reportChart {}
    export interface I_cloneBasePanelConf extends T_cloneBasePanelConf {}
    export interface I_cloneBasePanel extends T_cloneBasePanel {}
    export type I_compConfs =
      | I_baseGridConf
      | I_graphConf
      | I_treeGridConf
      | I_dateNavConf
      | I_baseTabConf
      | I_basePanelConf
      | I_cBaseFormConf
      | I_baseViewConf
      | I_refCompConf
      | I_multiFormConf
      | I_refGraphConf
      | I_refBaseGridConf
      | I_reportChartConf
      | I_cloneBasePanelConf;

    export type I_compClass =
      | I_graph
      | I_baseGrid
      | I_basePanel
      | I_treeGrid
      | I_dateNav
      | I_cBaseForm
      | I_baseView
      | I_refComp
      | I_multiForm
      | I_refGraph
      | I_refBaseGrid
      | I_reportChart
      | I_cloneBasePanel
      | I_baseTab;

    export interface I_tableStyle {
      showHeader: boolean;
      showFooter: boolean;
      showPager?: boolean;
      showToolbar: boolean;
      border: boolean;
      // eslint-disable-next-line no-use-before-define
      mode: TABLESTYLE_MODE;
    }

    export interface I_eventSearchMsg {
      type: 'search' | 'coordination';
      content: {
        ikey: string; // default 'id'
        ukey: string; // 要搜索的对应字段 例如:master_bill_id,
        val: any;
      };
      option?: {
        autoCompleteInfo?: {
          key: string;
          sourceToID: string;
          table: string; // 表与表对应设置M2o  表与表之间是来源表的意思 fromTable
          data: any;
        };
      };
    }
    export type T_eventCoordinationToolbar =
      | 'insert'
      | 'delete'
      | 'edit'
      | 'unEdit'
      | 'save'
      | 'audit'
      | 'refresh'
      | 'unAudit';
    // 协同事件消息
    export interface I_eventCoordinationMsg {
      type: 'coordination';
      fromComp: {
        funName: string;
        // 默认监听的协同事件ID
        fromEvenID: string;
      };
      action: {
        toolbar: T_eventCoordinationToolbar;
      };
    }
    export interface I_eventSearch {
      toID: string; // 要搜索的id 1.0定义的是功能名
      msg: I_eventSearchMsg;
    }
    export interface I_eventRefresh {}
    export interface I_custDefListener {
      search?: Array<string>;
      refresh?: Array<string>;
      // 组件组间配合监听事件
      coordination: string;
    }

    // 一个主功能里所有组件的信息，在代组件或者子组件
    // 中方便查找事件来处理消息协作
    export interface I_eventGroup {
      [funName: string]: {
        custDefListener: I_custDefListener;
        children: Array<I_eventGroup>;
      };
    }
    export interface I_messageConf {
      custDefListener: I_custDefListener;
      emitActions: {
        search: Array<I_eventSearch>;
        refresh: Array<I_eventRefresh>;
      };
      // 一个主功能里所有组件的信息，在代组件或者子组件
      // 中方便查找事件来处理消息协作
      eventGroup: I_eventGroup;
    }

    // 日期导航组件 统计字段规则配置
    export interface I_fieldCountRule {
      field: string;
      type: 'number' | 'string';
      conditions: Array<{
        operator: '>' | '>=' | '<' | '<=' | '==' | '!='; // 'has' | 'nhas'
        value: any;
      }>;
    }

    export interface I_setupCompSet {
      isShow: boolean;
      disabled: boolean;
      defVal: any;
    }
    export interface I_setupPanel {
      attrCurrent?: {
        currentTab?: I_setupCompSet;
        selectedComp?: I_setupCompSet;
        mainFun?: I_setupCompSet;
        historyComps?: I_setupCompSet;
      };
      attrDetail?: {
        currentTab?: I_setupCompSet;
        funName?: I_setupCompSet;
        funNote?: I_setupCompSet;
        refComp?: I_setupCompSet;
        cloneComp?: I_setupCompSet;
        multiForm?: I_setupCompSet;
        formStyle?: I_setupCompSet;
        editWindow?: I_setupCompSet;
        tableGraphMode?: I_setupCompSet;
        dbType?: I_setupCompSet;
        compLink?: I_setupCompSet;
        QBESearch?: I_setupCompSet;
        bancodeScan?: I_setupCompSet;
        tableStyle?: I_setupCompSet;
        toolbar?: I_setupCompSet;
        tableDataSetting?: I_setupCompSet;
        queryLink?: I_setupCompSet;
        selbill?: I_setupCompSet;
        action?: I_setupCompSet;
        rowDbClickSetting?: I_setupCompSet;
        customButton?: I_setupCompSet;
        group?: I_setupCompSet;
        dateNav?: I_setupCompSet;
        reportChart?: I_setupCompSet;
      };
      attrDetailTrigger?: {
        enabled?: I_setupCompSet;
        styleEdit?: I_setupCompSet;
        groupByField?: I_setupCompSet;
      };
      attrDetailToolbar?: {
        insert?: I_setupCompSet;
        default?: I_setupCompSet;
        delete?: I_setupCompSet;
        save?: I_setupCompSet;
        import?: I_setupCompSet;
        export?: I_setupCompSet;
        globalSearch?: I_setupCompSet;
        refresh?: I_setupCompSet;
        edit?: I_setupCompSet;
        print?: I_setupCompSet;
        audit?: I_setupCompSet;
        unaudit?: I_setupCompSet;
        antiAudit?: I_setupCompSet;
        openData?: I_setupCompSet;
        pasteColumn?: I_setupCompSet;
        jumpFirst?: I_setupCompSet;
        jumpLast?: I_setupCompSet;
        next?: I_setupCompSet;
        previous?: I_setupCompSet;
        search?: I_setupCompSet;
        requestAudit?: I_setupCompSet;
        auditStatus?: I_setupCompSet;
        revokeApproval?: I_setupCompSet;
        expandAll?: I_setupCompSet;
        contractAll?: I_setupCompSet;
        mutiSelect?: I_setupCompSet;
        open?: I_setupCompSet;
        exportXML?: I_setupCompSet;
        accountPosted?: I_setupCompSet;
        cancelPost?: I_setupCompSet;
        caseclosed?: I_setupCompSet;
        anticlosing?: I_setupCompSet;
        freeze?: I_setupCompSet;
        unfreeze?: I_setupCompSet;
        void?: I_setupCompSet;
        antivoid?: I_setupCompSet;
      };
      attrLibFeild?: {
        currentTab?: I_setupCompSet;
        refTable?: I_setupCompSet;
        selectField?: I_setupCompSet;
        uniqueIndexes?: I_setupCompSet;
        indexes?: I_setupCompSet;
        rules?: I_setupCompSet;
        headerGroup?: I_setupCompSet;
        charts?: I_setupCompSet;
        btnUnique?: I_setupCompSet;
        btnIndex?: I_setupCompSet;
        btnSelect?: I_setupCompSet;
        btnDelete?: I_setupCompSet;
        btnUp?: I_setupCompSet;
        btnDown?: I_setupCompSet;
        btnGetField?: I_setupCompSet;
      };
      attrLibFeildDetail?: {
        chkBoxIsShow?: I_setupCompSet;
        chkBoxIsNotNull?: I_setupCompSet;
        chkBoxDescField?: I_setupCompSet;
        chkBoxIsItnecessary?: I_setupCompSet;
        chkBoxProhibitinput?: I_setupCompSet;
        chkBoxDevFilter?: I_setupCompSet;
        chkBoxNodeData?: I_setupCompSet;
        chkBoxFixedLeft?: I_setupCompSet;
        chkBoxFixedRight?: I_setupCompSet;
        placeholder?: I_setupCompSet;
        fieldPrefix?: I_setupCompSet;
        fieldSuffix?: I_setupCompSet;
        inputDefVal?: I_setupCompSet;
        i18nFieldAlias?: I_setupCompSet;
        inputMinWidth?: I_setupCompSet;
        inputWidth?: I_setupCompSet;
        inputAlignment?: I_setupCompSet;
        inputAppSort?: I_setupCompSet;
        inputSummaryMethod?: I_setupCompSet;
        inputFormat?: I_setupCompSet;
        inputM2o?: I_setupCompSet;
        inputDropDownOptions?: I_setupCompSet;
        inputAuxiliaryAttributeField?: I_setupCompSet;
        inputEvent?: I_setupCompSet;
        inputEventValChange?: I_setupCompSet;
        inputSubQuery?: I_setupCompSet;
        inputInitFilter?: I_setupCompSet;
        inputDefFilter?: I_setupCompSet;
        inputInitSort?: I_setupCompSet;
        inputInitsortWeights?: I_setupCompSet;
        isNodeName?: I_setupCompSet;
        fieldProperties?: I_setupCompSet;
        fieldDataFormat?: I_setupCompSet;
        fieldAssociation?: I_setupCompSet;
        fieldEvevts?: I_setupCompSet;
        fieldSubquery?: I_setupCompSet;
        FieldFilteringAndSorting?: I_setupCompSet;
      };
      attrMenu?: {
        currentTab?: I_setupCompSet;
        navigationAndGroup?: I_setupCompSet;
        Navigation?: I_setupCompSet;
        group?: I_setupCompSet;
        parentFunction?: I_setupCompSet;
      };
    }
    export interface I_addon {
      checked: boolean;
      title: string;
      action: string;
      icon: string;
      children: Array<I_addon>;
    }
    export interface I_compToolbar {
      [key: string]: boolean | Array<I_addon>;
      addon?: Array<I_addon>;
    }

    /// ////////////////////////////////////////////////////////////////

    export enum COMP_NAME {
      BASE_GRID = 'BaseGrid', // 表格
      BASE_TAB = 'BaseTab', // 标签页
      BASE_PANEL = 'BasePanel', // 单据
      TREE_GRID = 'TreeGrid', // 树形导航
      DATE_NAV = 'DateNav', // 日期导航
      GRAPH = 'Graph', // 画布
      C_BASE_FORM = 'cBaseForm', // 表单
      BASE_VIEW = 'BaseView', // 视图
      REF_COMP = 'RefComp', // 引用组件
      MULTI_FORM = 'MultiForm', // 多表表单
      REF_GRAPH = 'RefGraph', // 引用画布
      REF_BASE_GRID = 'RefBaseGrid', // 引用实表
      REPORT_CHART = 'ReportChart', // 报表图表
      SPLIT_PANEL = 'SplitPanel', // 拆分面版
      CLONE_BASE_PANEL = 'CloneBasePanel' // 克隆单据
    }
    export enum COMP_TYPE {
      COMP = 'comp'
    }
    // 组件分类
    export enum COMP_CATEGORY {
      BASE_DATA = 'baseData', // 基础数据
      CONTAINER = 'container', // 容器
      SINGLE_FUN = 'singleFun', // 单功能组件
      GRAPH = 'graph', // 图形图表
      NAV = 'nav' // 导航
    }
    export enum COMP_MODULE_ID {
      BASE_GRID = '1',
      BASE_TAB = '2',
      BASE_PANEL = '3',
      TREE_GRID = '4',
      DATE_NAV = '5',
      GRAPH = '7',
      BASE_FORM = '8',
      BASE_VIEW = '9',
      REF_COMP = '10',
      MULTI_FORM = '11',
      REF_GRAPH = '12',
      REF_BASE_GRID = '13',
      REPORT_CHART = '14',
      SPLIT_PANEL = '15',
      CLONE_BASE_PANEL = '16'
    }
    export enum DB_TYPE {
      MYSQL = 'mysql',
      MONGO = 'mongo'
    }
    export enum TABLESTYLE_MODE {
      MODE_NAV = 'nav',
      MODE_NULL = null
    }

    export enum POSITION {
      POSITION_TOP = 'top',
      POSITION_BOTTOM = 'bottom',
      POSITION_LEFT = 'left',
      POSITION_RIGHT = 'right'
    }

    export enum TableType {
      TABLE,
      VIEW,
      VIRTUAL
    }

    export enum LAYOUTSTYLE {
      TABS = 'tabs',
      COLLAPSE = 'collapse'
    }

    export enum TABLE_TYPE {
      BASEGRID,
      BASEVIEW,
      VIRTUAL
    }

    export interface I_compItem<T> {
      moduleID: COMP_MODULE_ID;
      name: COMP_NAME;
      type: COMP_TYPE;
      category: N_oSetupPanel.N_comps.COMP_CATEGORY;
      config: T;
    }

    // export interface I_tableDataSet {
    //   editable: boolean;
    //   autoload: boolean;
    // }
  }
  class T_layoutConfComp {
    panels: Array<any>;

    tabStyle: object;
  }
  class T_panel {
    name: string;

    type: string;

    cellID: string;

    // eslint-disable-next-line no-use-before-define
    config: T_layoutConfComp | T_layoutConfLayout;

    moduleID?: string;
  }
  class T_layoutConfLayout {
    mode: string;

    // 排列
    value: number;

    // 比例 0.16
    first?: T_panel;

    secound?: T_panel;
  }

  class T_layout {
    layoutID: number;

    name: string;

    type: string;

    config: T_layoutConfComp | T_layoutConfLayout;
  }
  class T_field {
    libFieldID: string;

    notNull: boolean;

    comment: string;

    isDescription: boolean;

    m2oFun: string;

    footerMethod: any;

    selectionID: any;

    subQuery: any;

    isI18n: boolean;

    isFile: boolean;
  }

  class T_index {
    id: string;

    name: string;
  }

  class T_tableColumn {
    fieldID: string;

    // 字段库ID
    field: string;

    // 字段名
    type: string;

    // 字段类型
    isShow: boolean;

    // 是否显示
    notNull: boolean;

    // 不为空
    defValue: string;

    // 默认值
    comment: string;

    // 说明
    selectionID: any;

    disabled: boolean;

    defaultFilter: boolean;

    footerMethod: any;

    isDes: boolean;

    minWidth: string;

    subQuery: string;

    align: string;

    appWeight: string;

    auxProp: string;

    actions: Array<object>;

    nodeData: boolean;

    formatter: {
      type: string;
      decimal: number;
      thousandmark: boolean;
      currency: string;
    };

    many2one: {
      toTable: string;
      toField: string;
      isDropdown: boolean;
      customSearch: string;
      multiConnect: string;
      isFunLayout: boolean;
      fields: Array<any>;
    };
  }

  export class T_search {
    toID: string;

    // 源toID
    sourceToID: string;

    msg: {
      type: string;
      content: {
        ikey: string;
        ukey: string;
        val: string;
      };
    };
  }

  export interface I_permission {
    type: 'button' | 'columns' | 'selbill' | 'customButton';
    area: 'toolbar' | 'table' | 'form';
    data: Array<string>;
  }
  export interface I_index {
    indexField: Array<string>;
  }
  export interface I_custDefListener {
    search?: Array<string>;
    refresh?: Array<string>;
  }
  // export interface I_messageConfig {
  //   emitActions: {
  //     custDefListener?: I_custDefListener;
  //     seach: Array<T_search>;
  //     refresh: Array<any>;
  //   };
  // }
  export interface I_tableDataSetting {
    editable: boolean;
    autoload: boolean;
  }
  class T_fun {
    type: string;

    name: string;

    comment: string;

    status: string;

    fields: Array<T_field>;

    config: {
      name: string;
      config: {
        name: string;
        comment: string;
        status: string;
        isCreated: boolean;
        refTable?: string; // 引用功能
        tableStyle: object;
        tableColumns: Array<T_tableColumn>;
        indexes: Array<T_index>;
        uniqueIndexes: Array<T_index>;
        rules: Array<any>;
        selbill: Array<any>;
        queryLink: Array<any>;
        action: object;
        headerGroup: Array<any>;
        charts: Array<any>;
        toolbar: object;
        tableDataSetting: I_tableDataSetting;
        // messageConfig: I_messageConfig;
        messageConfig: N_comps.I_messageConf;
        actions: object;
        dbType: string; // 'mysql|oracle|mongoDB'
      };
    };

    uniqueIndexes: Array<I_index>;

    indexes: Array<I_index>;

    permissions: Array<I_permission>;

    refTable: any;
  }
  class T_menu {
    funName: string;

    navName: string;

    navID: string;

    // group: string;

    parent: string;
  }

  export class T_funConf {
    layout: T_layout;

    funs: Array<T_fun>;

    menu: T_menu;
  }
  export interface I_tableData {
    [key: string]: Array<any>;
  }
  export interface I_funConf extends T_funConf {}
  export interface I_editorTabsList {
    label: string;
    compName: string; // 如果是容器 compName='layout'
    tabsID: string; // tab组件ID
    cellID: string;
    config: any; // 容器内的layout
    isContainer: boolean; // 是否是容器
  }

  export interface oSetupPanel extends Vue {
    showAnimation: any;

    loading: boolean;

    showContextMenu: boolean;

    showPosition: { clientX: number; clientY: number };

    menuList: Array<any>;

    contextMenuCallback: Function;

    layoutsPanelPaneldraggable: boolean;

    compPanelDraggable: boolean;

    status: {
      isShow: boolean; // 是否渲染组件，未打开则不渲染
      isChange: boolean; // 是否变化了cell的选择，发送给其他组件信息
    };

    switcher: {
      main: number; // 顶部按钮的index，0为关闭所有，1为开启布局，2为开启组件
      attr: string; // 属性栏的切换
    };

    layout: {
      id: string; // 当前layoutID
      isSelected: boolean; // 是否选择了layout
      current: null; // 正在编辑的layout的配置
    };

    cell: {
      id: string; // 当前cellID
      isSelected: boolean; // 是否选择了cell
    };

    component: {
      current: any; // 正在编辑的组件配置
    };

    // 编辑的所有components的配置
    configStore: {
      all: any;
      stored: Array<any>;
    };

    mainLayoutName: any;

    recoverConfig: any; // 用于配置数据恢复的中间变量

    G_toolMap: Map<string, any>;

    G_list: Array<N_oSimpleToolbar.I_Conf>;

    editorconfig: any; // 用于修改模板clr

    layoutisadd: boolean; // 是否是模板新增clr

    layouts: Array<any>;
    // 生成最终json
    transformConfigToJson(): void;
    save();
    updateCompIDList(
      tabsID: string, // tabs组件ID
      currTabsIdx: number, // 当前tabslist 下标
      tabsList: N_oSetupPanel.I_editorTabsList[]
    );
    setupResetCell();
    setupOnCellFilled(type: any, componentID: any, cellID: any);
    setupOnCellSelected(cellID: any);
    findConfig();
    compDrop(e);
    addConfigStrore(
      componentID: string,
      cellID: string,
      compName?: N_oSetupPanel.N_comps.COMP_NAME
    );
    // 设置messageConfig
    configToMessageConfig(message: any, config: any): ifs.I_return;
    configToLibfield(config, compConfig, libfieldConfig);
    configToMenu(config);
    initCompIDList(tabsList: Array<N_oSetupPanel.I_editorTabsList>);
  }
}
